package controllers;

import entities.Archive;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
 *
 * @author Sergio
 */
public class fileReader {

    public void analyzeFile(String theFile) {

        try {
            SyntacticValidator validator = new SyntacticValidator();
            // Open the file that is the first 
            // command line parameter
            FileInputStream fstream = new FileInputStream(theFile);
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            //Read File Line By Line
            int correct = 0;
            int wrong = 0;
            long startTime = System.nanoTime();
            int count = 0;
            
            Archive archive = validator.getArchive(theFile, 1001, 1);
            
            while ((strLine = br.readLine()) != null) {
                count++;
                // Print the content on the console
                System.out.print(" Nice! ");
                if (validator.validate(strLine,archive,count)) {
                    correct++;
                    System.out.print(" Nice! ");
                    /*
                     * persist into validate trace - trace
                     */
                } else {
                    wrong++;
                    System.out.print(" Wrong ");
                }
                System.out.println(strLine);
            }

            System.out.println("Good ones: " + correct);
            System.out.println("Spoilt ones: " + wrong);
            System.out.println("Total:  " + (wrong + correct));
            long estimatedTime = System.nanoTime() - startTime;
            System.out.println("Time:  " + ((float) estimatedTime / (float) 1000000000) + " segundos");
            //Close the input stream
            in.close();
        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }

    }
}
